package com.zoke.dappstore.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zoke.dappstore.Conf;
import com.zoke.dappstore.R;
import com.zoke.dappstore.base.NebActivity;
import com.zoke.dappstore.dialog.AppLoading;
import com.zoke.dappstore.model.Comment;
import com.zoke.neb.tool.PersistTool;
import com.zoke.neb.tool.UToast;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import me.zhanghai.android.materialratingbar.MaterialRatingBar;

@ContentView(R.layout.activity_add)
public class AddActivity extends NebActivity {
    @ViewInject(R.id.et_name)
    EditText et_name;
    @ViewInject(R.id.et_content)
    EditText et_content;
    @ViewInject(R.id.rb)
    MaterialRatingBar rb;
    @ViewInject(R.id.tv_point)
    TextView tv_point;


    public static void open(Activity activity, String id) {
        Intent intent = new Intent(activity, AddActivity.class);
        intent.putExtra("id", id);
        activity.startActivity(intent);
    }

    private String id;

    private int ration;

    private AppLoading loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loading = new AppLoading(this, "提交中");
        id = getIntent().getStringExtra("id");
        LogUtil.e("id=" + id);
        mTitleBar.setTitle("添加评论");
        mTitleBar.setLeft(R.drawable.ic_back, null);
        rb.setProgress(10);
        ration = 5;
        tv_point.setText(ration + "分");
        rb.setOnRatingChangeListener((ratingBar, rating) -> {
            LogUtil.e("MaterialRatingBar =" + rating);
            ration = (int) rating;
            tv_point.setText(ration + "分");
        });
        String keyName = PersistTool.getString(Conf.KEY_NAME, "");
        if (!TextUtils.isEmpty(keyName)) {
            et_name.setText(keyName);
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        initBar(R.id.status, false);
    }

    @Event({R.id.btn_submit})
    private void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                postComment();
                break;
        }
    }

    private void postComment() {
        final String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            UToast.show(this, "用户名不能为空");
            return;
        }

        String content = et_content.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            UToast.show(this, "评论内容不能为空");
            return;
        }
        Comment comment = new Comment();
        comment.score = ration;
        comment.author = name;
        comment.content = content;
        String json = new Gson().toJson(comment);
        LogUtil.e("json=" + json);
        post("commentSubmit", new String[]{id, json}, new SmartCallback() {
            @Override
            public void onStart() {
                loading.show();
            }

            @Override
            public void onSuccess(String response) {
                LogUtil.e(response);
                PersistTool.saveString(Conf.KEY_NAME, name);
                //存储地址
                loading.dismiss();
                try {
                    JSONObject jo = new JSONObject(response);
                    String code = jo.getString("code");
                    if (code.equals("0")) {
                        JSONObject jsonObject = jo.getJSONObject("data");
                        String from = jsonObject.optString("from");
                        PersistTool.saveString(Conf.KEY, from);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                onBackPressed();
            }

            @Override
            public void onError(String error) {
                LogUtil.e(error);
                loading.dismiss();
            }
        });

    }
}
